package weaver.interfaces.schedule;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.lf5.util.StreamUtils;

import org.json.JSONException;


public class addhkzp extends BaseCronJob{
	
	public void execute() {

		try {
			 String result = GetCameraPreviewURL();
			 System.out.println("result���ʾ��: " + result);
		} catch (Exception e) {
			System.out.println("e======="+e.toString());
		}
	}
	

    public static String GetCameraPreviewURL() throws JSONException {

        
        ArtemisConfig.host = "172.18.62.253:443"; // artemis���ط�����ip�˿�
        ArtemisConfig.appKey = "27892547";  // ��Կappkey
        ArtemisConfig.appSecret = "9cKPLay9zjEQXwuE7xiQ";// ��ԿappSecret

        System.out.println("addhkzp1");
        final String ARTEMIS_PATH = "/artemis";

        /**
                                 按条件查询人脸抓拍事件ַ
         */
        final String getCamsApi = ARTEMIS_PATH + "/api/frs/v1/event/face_capture/search";
		/*
		 * Map<String, String> path = new HashMap<String, String>(2){
		 * 
		 * 
		 * private static final long serialVersionUID = -1719619937393482506L;
		 * 
		 * { System.out.println("addhkzp3"); put("https://", getCamsApi); }
		 * 
		 * };
		 */
        String result = "";
        try {
        String urls = "https://172.18.62.253:443/artemis/api/frs/v1/event/face_capture/search";
        URL url = new URL(urls);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
        connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
        connection.connect();
        
        System.out.println("addhkzp2"+getCamsApi);
        String contentType = "application/json";

        /**
         * STEP5����װ�������
         */
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'00:01:00XXX");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'23:59:00XXX");
        String s = sdf.format(new Date());
        String w = sdf1.format(new Date());
		 
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", "1");
        jsonBody.put("pageSize", "20");
        jsonBody.put("startTime",s);
		jsonBody.put("endTime", w);
		String body = jsonBody.toString();
		//String result =ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,contentType,null);// post请求application/json类型参数
		
		System.out.println("dlym传递的数据"+jsonBody);
		
		 
		OutputStreamWriter outs = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
		outs.append(jsonBody.toString());
		outs.flush();
		outs.close();

		
		int code = connection.getResponseCode();
		if (code == 200) {
		 // 连接获取数据
		 InputStream inputStream = connection.getInputStream();
		 // 读取数据
		 
		
			result = result + StreamUtils.getBytes(inputStream);
		
		 inputStream.close();
		 
		}
		System.out.println("addhkzp3"+result);
		 } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
        
		 ModelBean model = new ModelBean();
		 model.setModeid("64");// 建模id
		 model.setUserid("1");// 创建人id
        
        Map<String, String> mainTableMap = new HashMap<String, String>();
          
        
		
             mainTableMap.put("cameraIndexcode","cameraIndexcode");
		  
		 
		 
//		 Map<String, String> mainTableMap = new HashMap<String, String>();
//		 mainTableMap.put("cameraIndexcode",poinfo.getString("cameraIndexcode"));//监控点唯一标识
		 //mainTableMap.put("cameraName","cameraName");//监控点名称
		 //mainTableMap.put("gender","gender");//性别
		    		 
		 model.setMainFieldMap(mainTableMap);
		 System.out.println("addhkzp4");	
		 ModelUtils modelUtils = new ModelUtils();
		 
		 result = modelUtils.saveOrUpdateModel(model);
	     return result;
		// System.out.println("result-----------------------"+result);
	}

	}


